
$(function(){
    if($("#xn_c_index_275_wrap").length > 0){
        var $tUl = $("#xn_c_index_275_tUl");
        var $tLi = $tUl.children("li");
        var $cUl = $("#xn_c_index_275_cUl");
        var $cLi = $cUl.children("li");
        var $cWrap = $("#xn_c_index_275_cWrap");
        var $cMain = $(".xn_c_index_275_cMain");
        var currClass = "xn_c_index_275_tCurr";
        var $left = $("#xn_c_index_275_left");
        var $right = $("#xn_c_index_275_right");
        var $wrap = $("#xn_c_index_275_wrap");
        var speed = 500;
        var isAutoPlay = true;
        var timeInterval = 3000;
        $tLi.first().addClass(currClass);
        $cLi.each(function(){
            var _this = $(this);
            _this.attr("index",_this.index());
        });
        $cLi.css("width",$cWrap.outerWidth(true));
        $cUl.css("width",$cLi.outerWidth(true) * $cLi.length).css("position","relative");
        var idx = 1;
        var size = $cLi.size();
        //点击类别切换：
        $tLi.first().addClass(currClass);
        $("#xn_c_index_275_cImg img").load(function(){
            $cUl.css("height",$cLi.eq(0).outerHeight(true));
        });
        $tLi.click(function(){
            var _this = $(this);
            idx = _this.index()+1;
            $cUl.stop().animate({"left":"-"+$cLi.outerWidth(true) * _this.index()},speed);
            _this.addClass(currClass).siblings().removeClass(currClass);
        });
        $cMain.each(function(){
            $(this).css("width",$(this).children("li").length * $(this).children("li").outerWidth(true)+100);
        });
        //选项卡左右切换：
        function nextDiv(){
            if(idx < $cLi.length){
                $cUl.stop().animate({"left":"-"+$cLi.outerWidth(true)*idx},speed);
                $tLi.eq(idx).addClass(currClass).siblings().removeClass(currClass);
                idx++;
            }else{
                $cUl.stop().animate({"left":"-"+$cLi.outerWidth(true)*($cLi.length-1) - 100 +"px"},speed/$cLi.length);
                $cUl.animate({"left":"-"+$cLi.outerWidth(true)*($cLi.length-1)},speed/$cLi.length);
            }
        }
        function prevDiv(){
            if(idx > 1){
                idx--;
                $cUl.stop().animate({"left":"-"+$cLi.outerWidth(true)*(idx-1)});
                $tLi.eq(idx-1).addClass(currClass).siblings().removeClass(currClass);
            }else{
                $cUl.stop().animate({"left":100+"px"},speed/$cLi.length);
                $cUl.animate({"left":0},speed/$cLi.length);
            }
        }
        //点击箭头左右切换：
        $left.click(function(){
            nextImg();
            $(this).addClass('l_r_curr').siblings().removeClass('l_r_curr');
        });
        $right.click(function(){
            prevImg();
            $(this).addClass('l_r_curr').siblings().removeClass('l_r_curr');
        });
        function prevImg(){
            var _index = $(".xn_c_index_275_tCurr").index();
            var $items = $(".xn_c_index_275_cLi").eq(_index).children("ul");
            var $items_child_width = $items.children("li").outerWidth(true);
            $items.css("position","relative");
            $items.css('left',-$items_child_width);
            $items.prepend($items.children().last());
            $items.stop().animate({"left":0},speed);
        }
        function nextImg(){
            var _index = $(".xn_c_index_275_tCurr").index();
            var $items = $(".xn_c_index_275_cLi").eq(_index).children("ul");
            var $items_child_width = $items.children("li").outerWidth(true);
            // $items.css("position","relative");
            // $items.append($items.children().first());
            // $items.stop().animate({left:$items_child_width +"px"},speed,function(){
            //     $items.stop().animate({"left":0},speed);
            // });
            $items.css("position","relative");
            $items.css('left',$items_child_width);
            $items.append($items.children().first());
            $items.stop().animate({"left":0},speed);
        }
        var autoPlay = null;
        if(isAutoPlay == true){
            autoPlay = setInterval(nextImg,timeInterval);
            $wrap.hover(function(){
                clearInterval(autoPlay);
            },function(){
                autoPlay = setInterval(nextImg,timeInterval);
            });
        }
    }
});
